import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
//封装游客身份模块uuid-->生成一个随机字符串
import { getUUID } from '@/utils/uuid_token'
const state = {
    goodsInfo: {},
    // 游客临时身份
    uuid_token:getUUID()
}
const mutations = {
    GETGOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    }
}
const actions = {
    //获取产品信息的action
    async getGoodsInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code == 200) {
            commit("GETGOODSINFO",result.data)
        }
    },
    // 将产品添加到购物车
    // 注意:async函数执行返回结果一定是promise  (要么成功,要么失败)
    async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        // 返回的没有数据,所以不需要三连环
        // console.log(result);
        if (result.code == 200) {
            // 成功
            return 'ok'
        } else {
            // 失败
            return Promise.reject(new Error('faile'))
        }
    }
}
const getters = {
    // 简化数据
    //||{}的原因是goodsInfo一开始是一个空对象，还没有派发action，获取不到就用{}代替
    categoryView(state) {
        return state.goodsInfo.categoryView||{}
    },
    skuInfo(state) {
        return state.goodsInfo.skuInfo||{}
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList||[]
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}
